package org.itcast.adi.planinfo.service.impl;

import org.itcast.adi.planinfo.dao.PlanInfoMapper;
import org.itcast.adi.planinfo.entity.PageData;
import org.itcast.adi.planinfo.entity.PlanInfo;
import org.itcast.adi.planinfo.execption.CustomException;
import org.itcast.adi.planinfo.service.PlanInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class PlanInfoServiceImpl implements PlanInfoService {

    @Autowired
    private PlanInfoMapper planInfoMapper;

    @Override
    public int save(PlanInfo planInfo) {
        planInfo.setPlanId(UUID.randomUUID().toString());
        return planInfoMapper.savePlanInfo(planInfo);
    }

    @Override
    public PageData findByPage(int pageNum, int pageSize, PlanInfo planInfo) {
        if(pageNum<0 || pageSize < 0) {
            throw new CustomException(9999,"页码或页数不可小于0");
        }
        int startNum = (pageNum - 1) * pageSize;
        List<PlanInfo> planInfos = planInfoMapper.findByPage(startNum, pageSize, planInfo);
        int total = planInfoMapper.findByCount(planInfo);
        PageData pageData = new PageData();
        pageData.setPlanInfos(planInfos);
        pageData.setTotal(total);
        return pageData;
    }

}
